/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managerBeans;

import entity.Hospital;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.primefaces.context.RequestContext;
import sessionBeans.HospitalFacadeLocal;

/**
 *
 * @author Minh Luc
 */
@ManagedBean
@SessionScoped
public class HospitalManagement implements Serializable {

    @EJB
    private HospitalFacadeLocal hospitalFacade;
    private Hospital hospital = new Hospital();
    private Hospital selectHospital;
    private List<Hospital> list = null;
    private List<Hospital> filteredList = null;

    public void create() {

        if (!validate(hospital)) {
            return;
        }

        RequestContext context = RequestContext.getCurrentInstance();
        FacesMessage msg = null;
        try {
            hospitalFacade.create(hospital);
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Create Hospital", "Create Successful");
        } catch (Exception e) {
            msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", e.getMessage() + "Duplicate Name");
        }
        hospital = new Hospital();
        list = getHospitalList();
        FacesContext.getCurrentInstance().addMessage(null, msg);
        context.addCallbackParam("create", true);
    }

    public void edit() {

        if (!validate(selectHospital)) {
            return;
        }

        RequestContext context = RequestContext.getCurrentInstance();
        FacesMessage msg = null;

        try {
            hospitalFacade.edit(selectHospital);
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Edit Hospital", "Edit Successful");
        } catch (Exception e) {
            msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", e.getMessage());
        }
        list = getHospitalList();
        FacesContext.getCurrentInstance().addMessage(null, msg);
        context.addCallbackParam("edit", true);
    }

    private List<Hospital> getHospitalList() {
        if (System.user != null) {
            if (System.Manager) {
                return hospitalFacade.findAll();
            } else {
                return hospitalFacade.findByHospitalAction();
            }
        } else {
            return null;
        }

    }

    public Hospital getHospital() {
        return hospital;
    }

    public void setHospital(Hospital hospital) {
        this.hospital = hospital;
    }

    public Hospital getSelectHospital() {
        return selectHospital;
    }

    public void setSelectHospital(Hospital selectHospital) {
        this.selectHospital = selectHospital;
    }

    public List<Hospital> getList() {
        list = getHospitalList();
        return list;
    }

    public void setList(List<Hospital> list) {
        this.list = list;
    }

    public List<Hospital> getFilteredList() {
        return filteredList;
    }

    public void setFilteredList(List<Hospital> filteredList) {
        this.filteredList = filteredList;
    }

    //Validate Hospital
    public boolean validate(Hospital hospital) {
        RequestContext context = RequestContext.getCurrentInstance();
        FacesMessage msg = null;

        if (hospital.getHospitalName() == null || hospital.getHospitalName().length() == 0) {
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "HospitalName is not null");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return false;
        } else {
            if (hospital.getHospitalName().length() < 2 || hospital.getHospitalName().length() > 30) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "HospitalName is from 2 - 30 Characters");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
            if (!util.Validate.isContent(hospital.getHospitalName())) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "HospitalName is only Character");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
        }

        if (hospital.getAddress() == null || hospital.getAddress().length() == 0) {
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "Address is not null");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return false;
        } else {
            if (hospital.getAddress().length() < 2 || hospital.getAddress().length() > 300) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "Address is from 2 - 300 Characters");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
            if (!util.Validate.isContent(hospital.getAddress())) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "Address is only Character");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
        }

        if (hospital.getCity() == null || hospital.getCity().length() == 0) {
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "City is not null");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return false;
        } else {
            if (hospital.getCity().length() < 2 || hospital.getCity().length() > 30) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "City is from 2 - 30 Characters");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
            if (!util.Validate.isCharacter(hospital.getCity())) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "City is only Character");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
        }

        if (hospital.getRegion() == null || hospital.getRegion().length() == 0) {
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "Region is not null");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return false;
        } else {
            if (hospital.getRegion().length() < 2 || hospital.getRegion().length() > 30) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "Region is from 2 - 30 Characters");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
            if (!util.Validate.isCharacter(hospital.getRegion())) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "Region is only Character");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
        }

        if (hospital.getPostalCode() == null || hospital.getPostalCode().length() == 0) {
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "PostalCode is not null");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return false;
        } else {
            if (!util.Validate.isInt(hospital.getPostalCode())) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "PostalCode is only Number");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
            if (hospital.getPostalCode().length() != 5) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "PostalCode is only 5 Numbers");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
        }

        if (hospital.getOfficePhone() == null || hospital.getOfficePhone().length() == 0) {
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "OfficePhone is not null");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return false;
        } else {
            if (!util.Validate.isPhone(hospital.getOfficePhone())) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "OfficePhone is from 8 - 14 Numbers");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
        }
        return true;
    }
}
